package com.changhai.diabetes.action;

import java.util.List;

import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.model.PhysicalSurvey;
import com.changhai.diabetes.model.PhysicalSurveyResult;
import com.changhai.diabetes.model.QualitySurvey;
import com.changhai.diabetes.model.QualitySurveyResult;
import com.changhai.diabetes.model.SymptomsSurvey;
import com.changhai.diabetes.model.SymptomsSurveyResult;
import com.changhai.diabetes.service.SurveyService;

/**
 * 问卷调查表（生存质量特性、症候、体质分类）
 * 
 * @author Andy
 * 
 */
public class SurveyAction extends BaseAction {

	private static final long serialVersionUID = 4392936553263004630L;
	private OpertResultMsg opertMsg;
	private Long patientid;
	private List<QualitySurvey> qualitySurvey;
	private QualitySurveyResult qualityResult;
	private List<SymptomsSurvey> symptomsSurvey;
	private SymptomsSurveyResult symptomsResult;
	private List<PhysicalSurvey> physicalSurvey;
	private PhysicalSurveyResult physicalResult;
	private SurveyService surveyService;
	private Integer flag = 0;
	private Integer gender;
	private String answer;
	private String result;
	private Integer total;
	private Integer totala;
	private Integer totalb;
	private Integer totalc;
	private Integer totald;

	public String execute() throws Exception {
		return super.execute();
	}

	public String query() {
		return "input";
	}

	public String editQualitySurvey() {
		if (patientid != null && patientid > 0) {
			try {
				qualitySurvey = surveyService.searchQualitySurveyQuestions();
				qualityResult = surveyService.searchQualityAnswerByPatientId(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无生存质量特性信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditQualitySurvey() {
		if (patientid == null || patientid <= 0 || qualityResult == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				qualityResult.setTotal(total);
				qualityResult.setTotala(totala);
				qualityResult.setTotalb(totalb);
				qualityResult.setTotalc(totalc);
				qualityResult.setTotald(totald);
				qualityResult.setAnswer(answer);
				surveyService.saveEditQualityAnswer(qualityResult);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "质量调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editQualitySurvey();
	}

	public String saveNewQualitySurvey() {
		if (patientid == null || patientid <= 0 || qualityResult == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				qualityResult.setTotal(total);
				qualityResult.setTotala(totala);
				qualityResult.setTotalb(totalb);
				qualityResult.setTotalc(totalc);
				qualityResult.setTotald(totald);
				qualityResult.setAnswer(answer);
				surveyService.saveNewQualityAnswer(qualityResult);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "质量调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editQualitySurvey();
	}
	
	public String editSymptomsSurvey() {
		if (patientid != null && patientid > 0) {
			try {
				symptomsSurvey = surveyService.searchSymptomsSurveyQuestions();
				symptomsResult = surveyService.searchSymptomsAnswerByPatientId(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无症候信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditSymptomsSurvey() {
		if (patientid == null || patientid <= 0 || symptomsResult == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				symptomsResult.setResult(result);
				symptomsResult.setAnswer(answer);
				surveyService.saveEditSymptomsAnswer(symptomsResult);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "症候调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editSymptomsSurvey();
	}

	public String saveNewSymptomsSurvey() {
		if (patientid == null || patientid <= 0 || symptomsResult == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				symptomsResult.setResult(result);
				symptomsResult.setAnswer(answer);
				surveyService.saveNewSymptomsAnswer(symptomsResult);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "症候调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editSymptomsSurvey();
	}

	public String editPhysicalSurvey() {
		if (patientid != null && patientid > 0) {
			try {
				physicalSurvey = surveyService.searchPhysicalSurveyQuestions();
				physicalResult = surveyService.searchPhysicalAnswerByPatientId(patientid);
				gender = surveyService.getGenderForQuestion(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无体质分类信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditPhysicalSurvey() {
		if (patientid == null || patientid <= 0 || physicalResult == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				physicalResult.setResult(result);
				physicalResult.setAnswer(answer);
				surveyService.saveEditPhysicalAnswer(physicalResult);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "体质调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editPhysicalSurvey();
	}

	public String saveNewPhysicalSurvey() {
		if (patientid == null || patientid <= 0 || physicalResult == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				physicalResult.setResult(result);
				physicalResult.setAnswer(answer);
				surveyService.saveNewPhysicalAnswer(physicalResult);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "体质调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editPhysicalSurvey();
	}

	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}

	public Long getPatientid() {
		return patientid;
	}

	public void setPatientid(Long patientid) {
		this.patientid = patientid;
	}

	public SurveyService getSurveyService() {
		return surveyService;
	}

	public void setSurveyService(SurveyService surveyService) {
		this.surveyService = surveyService;
	}

	public Integer getGender() {
		return gender;
	}

	public void setGender(Integer gender) {
		this.gender = gender;
	}

	public List<PhysicalSurvey> getPhysicalSurvey() {
		return physicalSurvey;
	}

	public void setPhysicalSurvey(List<PhysicalSurvey> physicalSurvey) {
		this.physicalSurvey = physicalSurvey;
	}

	public PhysicalSurveyResult getPhysicalResult() {
		return physicalResult;
	}

	public void setPhysicalResult(PhysicalSurveyResult physicalResult) {
		this.physicalResult = physicalResult;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public List<QualitySurvey> getQualitySurvey() {
		return qualitySurvey;
	}

	public void setQualitySurvey(List<QualitySurvey> qualitySurvey) {
		this.qualitySurvey = qualitySurvey;
	}

	public QualitySurveyResult getQualityResult() {
		return qualityResult;
	}

	public void setQualityResult(QualitySurveyResult qualityResult) {
		this.qualityResult = qualityResult;
	}

	public List<SymptomsSurvey> getSymptomsSurvey() {
		return symptomsSurvey;
	}

	public void setSymptomsSurvey(List<SymptomsSurvey> symptomsSurvey) {
		this.symptomsSurvey = symptomsSurvey;
	}

	public SymptomsSurveyResult getSymptomsResult() {
		return symptomsResult;
	}

	public void setSymptomsResult(SymptomsSurveyResult symptomsResult) {
		this.symptomsResult = symptomsResult;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getTotala() {
		return totala;
	}

	public void setTotala(Integer totala) {
		this.totala = totala;
	}

	public Integer getTotalb() {
		return totalb;
	}

	public void setTotalb(Integer totalb) {
		this.totalb = totalb;
	}

	public Integer getTotalc() {
		return totalc;
	}

	public void setTotalc(Integer totalc) {
		this.totalc = totalc;
	}

	public Integer getTotald() {
		return totald;
	}

	public void setTotald(Integer totald) {
		this.totald = totald;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}
}
